package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsTypeDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.GoodsType;
import com.atguigu.jxc.entity.Unit;
import com.atguigu.jxc.service.GoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GoodsTypeServiceImpl implements GoodsTypeService {
    @Autowired
    private GoodsTypeDao goodsTypeDao;

    @Override
    public List<Map<String, Object>> getAllGoodsType(Integer pId) {
        //获取所有的分类数据
        //根据pid遍历树目录
        List<GoodsType> goodsTypeList = goodsTypeDao.getAllGoodsType(pId);
        if (goodsTypeList == null) {
            return null;
        }
        //把所有的子节点保存
        List<Map<String, Object>> objects = new ArrayList<>();
        //循环树目录
        goodsTypeList.forEach(goodsType -> {
            HashMap map = new HashMap<String, Object>();
            map.put("id", goodsType.getGoodsTypeId());
            map.put("text", goodsType.getGoodsTypeName());
            map.put("iconCls", "goods-type");
            HashMap map2 = new HashMap<String, Object>();
            map2.put("state", goodsType.getGoodsTypeState());
            map.put("attributes", map2);
            //递归自旋，进行子目录下级遍历GoodsTypeId
            List<Map<String, Object>> mapList = getAllGoodsType(goodsType.getGoodsTypeId());
            if (mapList != null) {
                map.put("children", mapList);
            }
            objects.add(map);
        });
        return objects;
    }
    //查询所有商品单位
    @Override
    public Map<String, Object> getGoodsUnit() {
        List<Unit> unitList= goodsTypeDao.selectGoodsUnit();
        Map<String, Object> map=new HashMap<>();
        map.put("rows",unitList);
        return map;
    }
    //获取所有商品信息
    @Override
    public Map<String, Object> goodsList(Integer page,
                                         Integer rows,
                                         String goodsName,
                                         Integer goodsTypeId) {
        //获取库存分页
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;

        List<Goods> goodsList = goodsTypeDao.selectGoodsList(offSet, rows, goodsName, goodsTypeId);
        map.put("total", String.valueOf(goodsList.size()));
        map.put("rows", goodsList);
        return map;
    }

    @Override
    public ServiceVO saveType(String goodsTypeName, Integer pId) {

        //如果供应商id为空添加数据
        this.goodsTypeDao.saveType(goodsTypeName, pId);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }


    @Override
    public ServiceVO deleteType(Integer goodsTypeId) {
        //删除分类
        this.goodsTypeDao.deleteType(goodsTypeId);
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }
}